// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TimerModel.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Visualization
{
    using System;

    /// <summary>
    /// This model class aggregates the relevant data from the timer agent in a way that is useful e.g. in an application status bar.
    /// </summary>
    public sealed class TimerModel : Model
    {
        /// <summary>
        /// True if the timer is paused. 
        /// </summary>
        private bool isPaused;

        /// <summary>
        /// True if the timer is fast forwarding or skipping. 
        /// </summary>
        private bool isSkipping;

        /// <summary>
        /// Seek time in milliseconds.
        /// </summary>
        private long seek;

        /// <summary>
        /// Gets or sets the time elapsed so far. 
        /// </summary>
        public TimeSpan ElapsedTime
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the delegate that controls the timer.
        /// </summary>
        public Action<bool, bool, bool, long> PauseSkipSeekDelegate
        {
            get;
            set;
        }        

        /// <summary>
        /// Gets or sets a value indicating whether the timer is paused. This is the databound property used by the UI.
        /// </summary>
        public bool IsPaused 
        {
            get 
            { 
                return this.isPaused; 
            }

            set 
            {
                this.isSkipping = this.isSkipping && !value;
                this.PauseSkipSeekDelegate(value, this.isSkipping, false, this.Seek);
                this.isPaused = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the timer is fast forwarding (or skipping). This is the databound property used by the UI.
        /// </summary>
        public bool IsSkipping
        {
            get
            {
                return this.isSkipping;
            }

            set
            {
                this.PauseSkipSeekDelegate(this.IsPaused, value, false, this.Seek);
                this.isSkipping = value && !this.IsPaused;
            }
        } 

        /// <summary>
        /// Gets or sets a value indicating the seek time in milliseconds for the timer.
        /// </summary>
        public long Seek
        {
            get
            {
                return this.seek;
            }

            set
            {
                if (value > this.ElapsedTime.TotalMilliseconds)
                {                    
                    // Pause the timer (don't use the property setter to avoid triggering the delegate callback.).
                    this.isPaused = true;
                    this.PauseSkipSeekDelegate(this.IsPaused, this.IsSkipping, true, value);
                    this.seek = value;                    
                }
            }
        }
    }
}
